package org.aphelie.torg.ruler.internal.ui.editor.character;

import org.aphelie.torg.ruler.core.character.ICharacterModelBase;
import org.aphelie.torg.ruler.internal.ui.TGRLabelProvider;
import org.aphelie.torg.ruler.internal.ui.TGRPlugin;
import org.aphelie.torg.ruler.internal.ui.TGRPluginImages;
import org.aphelie.torg.ruler.internal.ui.TGRUIMessages;
import org.aphelie.torg.ruler.internal.ui.editor.FormLayoutFactory;
import org.aphelie.torg.ruler.internal.ui.editor.TGRFormPage;
import org.aphelie.torg.ruler.internal.ui.util.SharedLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

public class OverviewPage extends TGRFormPage implements IHyperlinkListener {
	
	public static final String PAGE_ID = "overview"; //$NON-NLS-1$
	private GeneralInfoSection fInfoSection;
	private SketchSection fSketchSection;

	public OverviewPage(FormEditor editor) {
		super(editor, PAGE_ID, TGRUIMessages.OverviewPage_tabName);
	}
	
	protected void createFormContent(IManagedForm managedForm) {
		super.createFormContent(managedForm);
		ScrolledForm form = managedForm.getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		//if (isFragment()) {
		//	form.setImage(TGRPlugin.getDefault().getLabelProvider().get(TGRPluginImages.DESC_FRAGMENT_MF_OBJ));
		//} else {
			form.setImage(TGRPlugin.getDefault().getLabelProvider().get(TGRPluginImages.DESC_PLUGIN_MF_OBJ));
		//}
		form.setText(TGRUIMessages.ManifestEditor_OverviewPage_title);

		fillBody(managedForm, toolkit);
		//PlatformUI.getWorkbench().getHelpSystem().setHelp(form.getBody(), IHelpContextIds.MANIFEST_PLUGIN_OVERVIEW);
	}

	private void fillBody(IManagedForm managedForm, FormToolkit toolkit) {
		Composite body = managedForm.getForm().getBody();
		body.setLayout(FormLayoutFactory.createFormTableWrapLayout(true, 2));

		Composite left = toolkit.createComposite(body);
		left.setLayout(FormLayoutFactory.createFormPaneTableWrapLayout(false, 1));
		left.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
		//if (isFragment())
		//	fInfoSection = new FragmentGeneralInfoSection(this, left);
		//else
			//fInfoSection = new PluginGeneralInfoSection(this, left);
			fInfoSection = new IdentitySection(this, left);
			fSketchSection = new SketchSection(this, left);

		managedForm.addPart(fInfoSection);
		managedForm.addPart(fSketchSection);
		
		//CDO
		//managedForm.addPart(new IdentitySection(this, left));
		

		Composite right = toolkit.createComposite(body);
		right.setLayout(FormLayoutFactory.createFormPaneTableWrapLayout(false, 1));
		right.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
		createContentSection(managedForm, right, toolkit);
		/**
		if (isEditable() || getPDEEditor().hasInputContext(PluginInputContext.CONTEXT_ID))
			createExtensionSection(managedForm, right, toolkit);
		if (isEditable()) {
			createTestingSection(managedForm, isBundle() ? right : left, toolkit);
		}
		if (isEditable())
			createExportingSection(managedForm, right, toolkit);
			*/
	}
	
	private void createContentSection(IManagedForm managedForm, Composite parent, FormToolkit toolkit) {
		String sectionTitle;
		//if (isFragment()) {
		//	sectionTitle = PDEUIMessages.ManifestEditor_ContentSection_ftitle;
		//} else {
			sectionTitle = TGRUIMessages.ManifestEditor_ContentSection_title;
		//}
		Section section = createStaticSection(toolkit, parent, sectionTitle);

		Composite container = createStaticSectionClient(toolkit, section);

		FormText text = createClient(container, TGRUIMessages.OverviewPage_content, toolkit);
		TGRLabelProvider lp = TGRPlugin.getDefault().getLabelProvider();
		text.setImage("page", lp.get(TGRPluginImages.DESC_PAGE_OBJ, SharedLabelProvider.F_EDIT)); //$NON-NLS-1$

		if (true) {//(!isBundle() && isEditable()) {
			String content;
			//if (isFragment()) {
			//	content = PDEUIMessages.OverviewPage_fOsgi;
			//} else {
				content = TGRUIMessages.OverviewPage_osgi;
			//}
			text = createClient(container, content, toolkit);
		}
		section.setClient(container);
	}
	
	//TODO Move upper
	protected final Section createStaticSection(FormToolkit toolkit, Composite parent, String text) {
		Section section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR);
		section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
		section.setText(text);
		section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1));
		TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(data);
		return section;
	}
	
	//TODO Move upper
	protected final FormText createClient(Composite section, String content, FormToolkit toolkit) {
		FormText text = toolkit.createFormText(section, true);
		try {
			text.setText(content, true, false);
		} catch (SWTException e) {
			text.setText(e.getMessage(), false, false);
		}
		text.addHyperlinkListener(this);
		return text;
	}
	
	
	protected Composite createStaticSectionClient(FormToolkit toolkit, Composite parent) {
		Composite container = toolkit.createComposite(parent, SWT.NONE);
		container.setLayout(FormLayoutFactory.createSectionClientTableWrapLayout(false, 1));
		TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
		container.setLayoutData(data);
		return container;
	}
	
	private boolean isEditable() {
        ICharacterModelBase model = (ICharacterModelBase) getTGREditor().getContextManager().getAggregateModel();
        return model.isEditable();	    
	}
	

	@Override
	public void linkEntered(HyperlinkEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void linkExited(HyperlinkEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void linkActivated(HyperlinkEvent e) {
		String href = (String) e.getHref();
		// try page references
		if (href.equals("dependencies")) { //$NON-NLS-1$
			//getEditor().setActivePage(DependenciesPage.PAGE_ID);
		/**
		else if (href.equals("runtime")) //$NON-NLS-1$
			getEditor().setActivePage(RuntimePage.PAGE_ID);
		else if (href.equals("extensions")) { //$NON-NLS-1$
			if (getEditor().setActivePage(ExtensionsPage.PAGE_ID) == null)
				activateExtensionPages(ExtensionsPage.PAGE_ID);
		} else if (href.equals("ex-points")) { //$NON-NLS-1$
			if (getEditor().setActivePage(ExtensionPointsPage.PAGE_ID) == null)
				activateExtensionPages(ExtensionPointsPage.PAGE_ID);
		} else if (href.equals("build")) { //$NON-NLS-1$
			if (!getPDEEditor().hasInputContext(BuildInputContext.CONTEXT_ID)) {
				if (!MessageDialog.openQuestion(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.OverviewPage_buildTitle, PDEUIMessages.OverviewPage_buildQuestion))
					return;
				IFile file = PDEProject.getBuildProperties(getPDEEditor().getCommonProject());
				WorkspaceBuildModel model = new WorkspaceBuildModel(file);
				model.save();
				IEditorInput in = new FileEditorInput(file);
				getPDEEditor().getContextManager().putContext(in, new BuildInputContext(getPDEEditor(), in, false));
			}
			getEditor().setActivePage(BuildPage.PAGE_ID);
		} else if (href.equals("export")) { //$NON-NLS-1$
			getExportAction().run();
		} else if (href.equals("action.convert")) { //$NON-NLS-1$
			handleConvert();
		} else if (href.equals("organize")) { //$NON-NLS-1$
			getEditor().doSave(null);
			OrganizeManifestsAction organizeAction = new OrganizeManifestsAction();
			organizeAction.runOrganizeManfestsAction(new StructuredSelection(getPDEEditor().getCommonProject()));
		} else if (href.equals("externalize")) { //$NON-NLS-1$
			getEditor().doSave(null);
			GetNonExternalizedStringsAction externalizeAction = new GetNonExternalizedStringsAction();
			externalizeAction.runGetNonExternalizedStringsAction(new StructuredSelection(getPDEEditor().getCommonProject()));
		*/ } else {
			//TODO LaunchShortcut
			//super.linkActivated(e);
		}
		
	}
	
}
